﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using System.Collections.Generic;

public partial class UI_Search : System.Web.UI.Page
{
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        this.ddlLayers.Attributes.Add("onChange", "return onSelectedIndexChanged();");
    }    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) 
        {
            this.DataBind();
        }
    }

    public override void DataBind()
    {
        base.DataBind();
        IList<Bintje.Layer> layers = Bintje.Layer.GetLayers(Session["Session"] as Bintje.bintje.imageoSession);
        if (null != layers && layers.Count > 0)
        {               
            var lrs = (from l in layers
                       orderby l.Id
                       select new {Id = 0, LayerName = string.Empty }).ToArray().Union(
                (from l in layers
                       orderby l.Id
                       select new { l.Id, l.LayerName }).ToArray());
            if (null != lrs)
            {
                ddlLayers.DataSource = lrs;
                ddlLayers.DataValueField = "Id";
                ddlLayers.DataTextField = "LayerName";
                ddlLayers.DataBind();
            }
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Bintje.TrailFilter tf = new Bintje.TrailFilter();
        tf.Description = txtDescription.Text;
        tf.LayerNames = txtLayers.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        tf.MaxTrails = 20;
        tf.Radius = txtRadius.Text;
        tf.TrailTitle = txtTrailTitle.Text;
        tf.UserLogin = txtNickname.Text;
        Session["Filter"] = tf;
        Response.Redirect("TrailList.aspx?keywords=" + txtTrailTitle.Text.Replace(" ", "+"));
    }
    
    /*
    protected void ddlLayers_SelectedIndexChanged(object sender, EventArgs e)
    {
        string layer = ddlLayers.SelectedItem.Text;
        if (string.IsNullOrEmpty(layer))
            return;
        if (!(txtLayers.Text.IndexOf(layer) >= 0))
        {
            if (txtLayers.Text.Length > 0)
                txtLayers.Text += "," + layer;
            else
                txtLayers.Text = layer;
        }
    }
    */
}
